package thomas.studio.spring.security.ldap.service;

import java.util.Set;

import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;

public class User extends LdapUserDetailsImpl{
	
	private static final long serialVersionUID = -903206904770597340L;
	private String telephoneNumber;
	
	private String mail;
	
	private String countryCode;
	private String title;
	private String givenName;
	private String sn;
	private String department;
	private String cn;
	private String displayName;
	private String name;
	private String accountName;
	
	public String getTelephoneNumber() {
		return telephoneNumber;
	}

	public void setTelephoneNumber(String telephoneNumber) {
		this.telephoneNumber = telephoneNumber;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getCountryCode() {
		return countryCode;
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getGivenName() {
		return givenName;
	}

	public void setGivenName(String givenName) {
		this.givenName = givenName;
	}

	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getCn() {
		return cn;
	}

	public void setCn(String cn) {
		this.cn = cn;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public User() {		
		super();
	}
	
	public static class Essence extends LdapUserDetailsImpl.Essence{
		
			public Essence(){
				super();
			}
			
			public Essence(DirContextOperations context){
				super(context);
				//this.setRealname(context.getStringAttribute("cn"));
				this.setTelephoneNumber(context.getStringAttribute("telephoneNumber"));
				this.setMail(context.getStringAttribute("mail"));
				this.setCountryCode(context.getStringAttribute("countryCode"));
				this.setTitle(context.getStringAttribute("title"));
				this.setGivenName(context.getStringAttribute("givenName"));
				this.setSn(context.getStringAttribute("sn"));
				this.setDepartment(context.getStringAttribute("department"));
				this.setCn(context.getStringAttribute("cn"));
				this.setDisplayName(context.getStringAttribute("displayName"));
				this.setName(context.getStringAttribute("name"));
				this.setSAMAccountName(context.getStringAttribute("sAMAccountName"));
				this.setUsername(context.getStringAttribute("sAMAccountName"));
				// we use user's member of to set up user's roles
				//e.g. when memberOf=CN=sale,OU=Buisness,DC=thomas,DC=com
				// user will got role: ROLE_SALE
				Set<String> groups = context.getAttributeSortedStringSet("memberOf");
				if(groups != null) {
					for(String group: groups) {
						for(String role : RoleMapping.findRolesForGroup(group)) {
							this.addAuthority(new SimpleGrantedAuthority(role));
						}
					}
				}
				//user will got USER role by default 
				this.addAuthority(new SimpleGrantedAuthority(RoleMapping.ROLE_PREFIX + "USER"));
			}
			

			


			/**
			 * make sure (User)this.instance could work 
			 */
			@Override
			protected final LdapUserDetailsImpl createTarget(){
				return new User();
			}
			
			
			@Override
			public LdapUserDetails createUserDetails(){
				//LdapUserDetails userDetails = super.createUserDetails();
				User user = (User) super.createUserDetails();
				return user;
			}
			

			private void setTelephoneNumber(String telephoneNumber) {
				((User)this.instance).telephoneNumber = telephoneNumber;
				
			}
			
			public void setMail(String mail) {
				((User)this.instance).mail = mail;
			}			

			private void setSAMAccountName(String sAMAccountName) {
				((User)this.instance).accountName = sAMAccountName;
				
			}

			private void setName(String name) {
				((User)this.instance).name = name;
				
			}

			private void setCn(String cn) {
				((User)this.instance).cn = cn;
				
			}

			private void setDisplayName(String displayName) {
				((User)this.instance).displayName = displayName;
				
			}

			private void setDepartment(String department) {
				((User)this.instance).department = department;
				
			}

			private void setGivenName(String givenName) {
				((User)this.instance).givenName = givenName;
				
			}

			private void setSn(String sn) {
				((User)this.instance).sn = sn;
				
			}

			private void setTitle(String title) {
				((User)this.instance).title = title;
				
			}

			private void setCountryCode(String countryCode) {
				((User)this.instance).countryCode = countryCode;
				
			}


		}
	
}
